using System;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;

public class HealthBarController : MonoBehaviour
{
   private CharacterBase currentCharacter;
   [Header("Elements")]
   public Transform healthBarTransform;
   private UIDocument healthBarDocument;
   private ProgressBar healthBar;
   
   //防御的UI
   private VisualElement defenseElement;
   private Label defenseAmountLabel;
   //力量的UI
   private VisualElement buffElement;
   private Label buffRoundLabel;

   [Header("Buff图片")]
   public Sprite buffSprite;
   public Sprite debuffSprite;
   
   //敌人UI相关
   private Enemy enemy;
   private VisualElement intent;
   private Label intentAmountLabel;

   private void Awake()
   {
      currentCharacter = GetComponent<CharacterBase>();
      enemy = GetComponent<Enemy>();
   }

   private void OnEnable()
   {
      InitHealthBar();
   }

   private void MoveToWorldPosition(VisualElement element,Vector3 worldPosition,Vector2 size)
   {
      Rect rect = RuntimePanelUtils.CameraTransformWorldToPanelRect(element.panel, worldPosition, size, Camera.main);
      element.transform.position = rect.position;
   }

   [ContextMenu("测试血条")]
   public void InitHealthBar()
   {
      healthBarDocument = GetComponent<UIDocument>();
      healthBar = healthBarDocument.rootVisualElement.Q<ProgressBar>("HealthBar");
      healthBar.highValue = currentCharacter.MaxHp;
      //MoveToWorldPosition(healthBar, healthBarTransform.position, Vector2.zero);
      
      //赋值防御UI
      defenseElement = healthBar.Q<VisualElement>("Defense");
      defenseAmountLabel = defenseElement.Q<Label>("DefenseAmount");
      defenseElement.style.display = DisplayStyle.None;
      //赋值力量UI
      buffElement = healthBar.Q<VisualElement>("Buff");
      buffRoundLabel = buffElement.Q<Label>("BuffRound");
      buffElement.style.display = DisplayStyle.None;
      //赋值敌人UI
      intent = healthBar.Q<VisualElement>("Intent");
      intentAmountLabel = intent.Q<Label>("IntentAmount");
      intent.style.display = DisplayStyle.None;
   }

   public void MoveHealthBar()
   {
      MoveToWorldPosition(healthBar, healthBarTransform.position, Vector2.zero);
   }

   private void Update()
   {
      UpdateHelthBar();
   }

   public void UpdateHelthBar()
   {
      if (currentCharacter.isDead)
      {
         //关闭血条
         healthBar.style.display = DisplayStyle.None;
         return;
      }

      if (healthBar != null)
      {
         healthBar.title = $"{currentCharacter.currentHp}/{currentCharacter.MaxHp}";
         healthBar.value = currentCharacter.currentHp;
         
         healthBar.RemoveFromClassList("highHealth");
         healthBar.RemoveFromClassList("mediumHealth");
         healthBar.RemoveFromClassList("lowHealth");
         
         var percent = (float)currentCharacter.currentHp / currentCharacter.MaxHp;
         if (percent < 0.3f)
         {
            healthBar.AddToClassList("lowHealth");
         }
         else if (percent < 0.6f)
         {
            healthBar.AddToClassList("mediumHealth");
         }
         else
         {
            healthBar.AddToClassList("highHealth");
         }
         
         //设置防御UI的显示与数量
         var currentDefense = currentCharacter.defense.currentValue;
         defenseElement.style.display = currentDefense> 0 ? DisplayStyle.Flex : DisplayStyle.None;
         defenseAmountLabel.text = currentDefense.ToString();
         
         //设置力量UI的显示与数量
         var currentBuffRound = currentCharacter.buffRound.currentValue;
         buffElement.style.display = currentBuffRound > 0 ? DisplayStyle.Flex : DisplayStyle.None;
         buffRoundLabel.text = currentBuffRound.ToString();
         buffElement.style.backgroundImage = currentCharacter.baseStrength > 1 ? new StyleBackground(buffSprite):new StyleBackground(debuffSprite);
         
      }
   }

   /// <summary>
   /// 玩家回合开始时调用
   /// </summary>
   public void SetIntentElemt()
   {
      intent.style.display = DisplayStyle.Flex;
      intent.style.backgroundImage = new StyleBackground(enemy.currentAction.intentSprite);
      
      //判断是否为攻击
      var value = enemy.currentAction.effect.value;
      if (enemy.currentAction.GetType() == typeof(DamageEffect))
      {
         value = (int)math.round(enemy.currentAction.effect.value * enemy.baseStrength);
      }

      intentAmountLabel.text = value.ToString();

      // if (buffElement.style.display == DisplayStyle.Flex)
      //    enemy.UpdateStrengthRound();

   }
   /// <summary>
   /// 敌人回合结束时调用
   /// </summary>
   public void HideIntentElement()
   {
      intent.style.display = DisplayStyle.None;
   }
}
